

currentPath=$(pwd)

cat << EOF > $K8_CONFIG_HOME/etcd.conf
##[Member]
#ETCD_NAME="--name=$K8_HOST_ETCD_NAME"
#ETCD_DATA_DIR="--data-dir=/data1/etcd"
#ETCD_LISTEN_PEER_URLS="--listen-peer-urls=https://$K8_HOST:2380"
#ETCD_LISTEN_CLIENT_URLS="--listen-client-urls=https://$K8_HOST:2379"
# 
##[Clustering]
#ETCD_INITIAL_ADVERTISE_PEER_URLS="--initial-advertise-peer-urls=https://$K8_HOST:2380"
#ETCD_ADVERTISE_CLIENT_URLS="--advertise-client-urls=https://$K8_HOST:2379"
#ETCD_INITIAL_CLUSTER="--initial-cluster=$K8_ETCD_INIT_CLUSTER"
#ETCD_INITIAL_CLUSTER_TOKEN="--initial-cluster-token=etcd-cluster"
#ETCD_INITIAL_CLUSTER_STATE="--initial-cluster-state=new"
#
##[Security]
#ETCD_CERT_FILE="--cert-file=$K8_CERT_HOME/etcd-server.pem"
#ETCD_KEY_FILE="--key-file=$K8_CERT_HOME/etcd-server-key.pem"
#ETCD_TRUSTED_CA_FILE="--trusted-ca-file=$K8_CERT_HOME/etcd-ca.pem"
#ETCD_CLIENT_CERT_AUTH="--client-cert-auth=true"
#ETCD_PEER_CERT_FILE="--peer-cert-file=$K8_CERT_HOME/etcd-server.pem"
#ETCD_PEER_KEY_FILE="--peer-key-file=$K8_CERT_HOME/etcd-server-key.pem"
#ETCD_PEER_TRUSTED_CA_FILE="--peer-trusted-ca-file=$K8_CERT_HOME/etcd-ca.pem"
#ETCD_PEER_CLIENT_CERT_AUTH="--peer-client-cert-auth=true"

#[Member]
ETCD_NAME="$K8_HOST_ETCD_NAME"
ETCD_DATA_DIR="/data1/etcd"
ETCD_LISTEN_PEER_URLS="https://$K8_HOST:2380"
ETCD_LISTEN_CLIENT_URLS="https://$K8_HOST:2379"
 
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://$K8_HOST:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://$K8_HOST:2379"
ETCD_INITIAL_CLUSTER="$K8_ETCD_INIT_CLUSTER"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"

#[Security]
ETCD_CERT_FILE="$K8_CERT_HOME/etcd-server.pem"
ETCD_KEY_FILE="$K8_CERT_HOME/etcd-server-key.pem"
ETCD_TRUSTED_CA_FILE="$K8_CERT_HOME/etcd-ca.pem"
ETCD_CLIENT_CERT_AUTH="true"
ETCD_PEER_CERT_FILE="$K8_CERT_HOME/etcd-server.pem"
ETCD_PEER_KEY_FILE="$K8_CERT_HOME/etcd-server-key.pem"
ETCD_PEER_TRUSTED_CA_FILE="$K8_CERT_HOME/etcd-ca.pem"
ETCD_PEER_CLIENT_CERT_AUTH="true"

EOF


cat << EOF > $currentPath/build/service/etcd.service
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
Type=notify
WorkingDirectory=/data1/etcd/
EnvironmentFile=$K8_CONFIG_HOME/etcd.conf
# set GOMAXPROCS to number of processors
ExecStart=/bin/bash -c "GOMAXPROCS=\$(nproc) \
    $K8_ETCD_BIN/etcd --name=\\"\${ETCD_NAME}\\" --data-dir=\\"\${ETCD_DATA_DIR}\\" --listen-client-urls=\\"\${ETCD_LISTEN_CLIENT_URLS}\\" --listen-peer-urls=\\"\${ETCD_LISTEN_PEER_URLS}\\" --advertise-client-urls=\\"\${ETCD_ADVERTISE_CLIENT_URLS}\\" --initial-cluster-token=\\"\${ETCD_INITIAL_CLUSTER_TOKEN}\\" --initial-cluster=\\"\${ETCD_INITIAL_CLUSTER}\\" --initial-cluster-state=\\"\${ETCD_INITIAL_CLUSTER_STATE}\\" --cert-file=\\"\${ETCD_CERT_FILE}\\" --key-file=\\"\${ETCD_KEY_FILE}\\" --trusted-ca-file=\\"\${ETCD_TRUSTED_CA_FILE}\\" --client-cert-auth=\\"\${ETCD_CLIENT_CERT_AUTH}\\" --peer-cert-file=\\"\${ETCD_PEER_CERT_FILE}\\" --peer-key-file=\\"\${ETCD_PEER_KEY_FILE}\\" --peer-trusted-ca-file=\\"\${ETCD_PEER_TRUSTED_CA_FILE}\\" --peer-client-cert-auth=\\"\${ETCD_PEER_CLIENT_CERT_AUTH}\\""
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target



EOF

sed -i '/etcd\.service/d' $K8_NODE_SHELL
sed -i "/daemon-reload/i\cp $currentPath/build/service/etcd.service $K8_SERVICE_HOME/" $K8_NODE_SHELL
sed -i '/enable etcd/d' $K8_NODE_SHELL
sed -i '/start etcd/d' $K8_NODE_SHELL

echo "mkdir -p /data1/etcd" >> $K8_NODE_SHELL
echo "systemctl enable etcd" >> $K8_NODE_SHELL
echo "systemctl stop etcd" >> $K8_NODE_SHELL
echo "systemctl start etcd" >> $K8_NODE_SHELL
initShell=$currentPath/build/etcd-init.sh
echo "#etcd init backend " > $initShell
chmod +x $initShell
echo "$K8_ETCD_BIN/etcdctl --ca-file=$K8_CERT_HOME/etcd-ca.pem --cert-file=$K8_CERT_HOME/etcd-server.pem --key-file=$K8_CERT_HOME/etcd-server-key.pem --endpoints=\"$K8_ETCD_SERVERS\" set /k8s/network/config '{\"NetWork\":\"$K8_CLUSTER_IP_RANGE\",\"Backend\":{\"Type\":\"vxlan\"}} ' " >> $initShell

checkShell=$currentPath/build/etcd-check.sh
echo "# etcd check shell " > $checkShell
chmod +x $checkShell
echo "$K8_ETCD_BIN/etcdctl --ca-file=$K8_CERT_HOME/etcd-ca.pem --cert-file=$K8_CERT_HOME/etcd-server.pem --key-file=$K8_CERT_HOME/etcd-server-key.pem --endpoints=\"$K8_ETCD_SERVERS\" cluster-health" >> $checkShell

